<?php

namespace App\Http\Controllers\Admin;

use App\Models\Organization;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Models\Courses;
use App\Models\CourseScore;
use Illuminate\Support\Facades\DB;

class CourseScoreController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        if(!$this->user->can('viewTeachesComment')) abort(405);

        /*根据课程、评分查询评分记录*/
        $c_id = $request->input('course', 0);
        $score = $request->input('score');
        $score = !empty($score) ? $score : '';
        $oid = $request->input('organization', 0);
        $where_arr = [];
        if ($request->input('course', 0))
            $where_arr['c_id'] = $c_id;
        if ($score)
            $where_arr['score'] = $score;
        $where_arr['to_comment_id'] = 0;
        $organizations = Organization::all();
        $course = [];
        if ($oid > 0)
            $course = Organization::find($oid)->allclass;
        $scores = CourseScore::where($where_arr)->paginate(20);
        foreach ($scores as $key => $val) {
            $scores[$key]['cname'] = Courses::find($val->c_id)['name'];
        }
        return view('admin.coursescore.index')
                ->with('score', $scores)
                ->with('qurey_score', $score)
                ->with('courses', $course)
                ->with('cid', $c_id)
                ->with('oid', $oid)
                ->with('organizations', $organizations);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Request $request)
    {
//        if(!$this->user->can('deleteTeachesComment'))
//            return $this->formatJson('error',$this->permerror);
        $id = $request->input('id');
        $course_score = CourseScore::find($id);
        if(empty($course_score))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            DB::beginTransaction();
            try{
                //删除子评论,平均分计算
                CourseScore::where('to_comment_id', '=', $course_score->id)->delete();
                $course_score->delete();
                $scoreavg = DB::table('course_score')->where('c_id','=',$course_score->c_id)->avg('score');
                $effectavg = DB::table('course_score')->where('c_id','=',$course_score->c_id)->avg('effect');
                $teasavg = DB::table('course_score')->where('c_id','=',$course_score->c_id)->avg('teachers');
                $envavg = DB::table('course_score')->where('c_id','=',$course_score->c_id)->avg('environment');
                $courses = Courses::find($course_score->c_id);
                if(!empty($courses)){
                    $courses->score = $scoreavg;
                    $courses->effect = $effectavg;
                    $courses->teachers = $teasavg;
                    $courses->environment = $envavg;
                    $courses->save();
                }
                DB::commit();
            }catch (\PDOException $e){
                DB::rollback();
                return $this->formatJson('falied','评论删除失败');
            }
            return $this->formatJson('success','评论删除成功！');
        }
    }

    /**
     * 查询机构下的课程
     * @param Request $request
     * @return mixed
     */
    public function query(Request $request)
    {
        $o_id = $request->input('id');
        $org = Organization::find($o_id);
        $course_of_org = $org->allclass;
        return Response::json(array('status'=>'success','msg'=>'', 'data' => $course_of_org));
    }
}
